<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>选择器 | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/frontend_css_选择器.md.BRHA0Y77.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>CSS</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>CSS</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/css%E5%9F%BA%E7%A1%80.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>基础知识</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%8F%98%E5%BD%A2%E5%8A%A8%E7%94%BB.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>变形动画</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%93%8D%E5%BA%94%E5%BC%8F.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>响应式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%AA%92%E4%BD%93%E6%9F%A5%E8%AF%A2.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>媒体查询</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%AE%9A%E4%BD%8D%E5%B8%83%E5%B1%80.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>定位布局</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%B8%A7%E5%8A%A8%E7%94%BB.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>帧动画</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E5%BC%B9%E6%80%A7%E5%B8%83%E5%B1%80.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>弹性布局</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E6%95%B0%E6%8D%AE%E6%A0%B7%E5%BC%8F.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>数据样式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E6%96%87%E6%9C%AC.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>文本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E6%A0%85%E6%A0%BC%E7%B3%BB%E7%BB%9F.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>栅格系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E6%B5%AE%E5%8A%A8%E5%B8%83%E5%B1%80.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>浮动布局</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E7%9B%92%E5%AD%90%E6%A8%A1%E5%9E%8B.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>盒子模型</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E8%83%8C%E6%99%AF.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>背景</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/css/%E9%80%89%E6%8B%A9%E5%99%A8.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>选择器</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _frontend_css_%E9%80%89%E6%8B%A9%E5%99%A8" data-v-39a288b8><div><h1 id="选择器" tabindex="-1">选择器 <a class="header-anchor" href="#选择器" aria-label="Permalink to &quot;选择器&quot;">​</a></h1><p>CSS 选择器用于“查找”（或选取）要设置样式的 HTML 元素。</p><p>我们可以将 CSS 选择器分为五类：</p><ul><li>简单选择器（根据名称、id、类来选取元素）</li><li><a href="https://www.w3school.com.cn/css/css_combinators.asp" target="_blank" rel="noreferrer">组合器选择器</a>（根据它们之间的特定关系来选取元素）</li><li><a href="https://www.w3school.com.cn/css/css_pseudo_classes.asp" target="_blank" rel="noreferrer">伪类选择器</a>（根据特定状态选取元素）</li><li><a href="https://www.w3school.com.cn/css/css_pseudo_elements.asp" target="_blank" rel="noreferrer">伪元素选择器</a>（选取元素的一部分并设置其样式）</li><li><a href="https://www.w3school.com.cn/css/css_attribute_selectors.asp" target="_blank" rel="noreferrer">属性选择器</a>（根据属性或属性值来选取元素）</li></ul><h2 id="选择器-1" tabindex="-1">选择器 <a class="header-anchor" href="#选择器-1" aria-label="Permalink to &quot;选择器&quot;">​</a></h2><h3 id="简单选择器" tabindex="-1">简单选择器 <a class="header-anchor" href="#简单选择器" aria-label="Permalink to &quot;简单选择器&quot;">​</a></h3><table tabindex="0"><thead><tr><th style="text-align:left;">选择器</th><th style="text-align:left;">示例</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;">#id</td><td style="text-align:left;">#firstname</td><td style="text-align:left;">选择 id=&quot;firstname&quot; 的所有元素</td></tr><tr><td style="text-align:left;">.class</td><td style="text-align:left;">.intro</td><td style="text-align:left;">选择 class=&quot;intro&quot; 的所有元素</td></tr><tr><td style="text-align:left;">*</td><td style="text-align:left;">*</td><td style="text-align:left;">选择所有元素</td></tr><tr><td style="text-align:left;">element</td><td style="text-align:left;">p</td><td style="text-align:left;">选择所有 p 元素</td></tr><tr><td style="text-align:left;">element,element</td><td style="text-align:left;">div,p</td><td style="text-align:left;">选择所有 div 元素和所有 p 元素</td></tr><tr><td style="text-align:left;">element element</td><td style="text-align:left;">div p</td><td style="text-align:left;">选择 div 元素内部的所有 p 元素</td></tr><tr><td style="text-align:left;">element&gt;element</td><td style="text-align:left;">div&gt;p</td><td style="text-align:left;">选择父元素为 div 元素的所有 p 元素</td></tr><tr><td style="text-align:left;">element+element</td><td style="text-align:left;">div+p</td><td style="text-align:left;">选择紧接在 div 元素之后的 p 元素</td></tr></tbody></table><h3 id="组合选择器" tabindex="-1">组合选择器 <a class="header-anchor" href="#组合选择器" aria-label="Permalink to &quot;组合选择器&quot;">​</a></h3><table tabindex="0"><thead><tr><th style="text-align:left;">选择器</th><th style="text-align:left;">示例</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;">element element</td><td style="text-align:left;">div p</td><td style="text-align:left;">选择 div 元素内部的所有 p 元素</td></tr><tr><td style="text-align:left;">element&gt;element</td><td style="text-align:left;">div&gt;p</td><td style="text-align:left;">选择父元素为 div 元素的所有 p 元素</td></tr><tr><td style="text-align:left;">element+element</td><td style="text-align:left;">div+p</td><td style="text-align:left;">选择紧接在 div 元素之后的 p 元素</td></tr><tr><td style="text-align:left;">element~element2</td><td style="text-align:left;">p~ul</td><td style="text-align:left;">选择 p 元素同级并在 p 元素后面的所有 ul 元素</td></tr></tbody></table><h3 id="伪类选择器" tabindex="-1">伪类选择器 <a class="header-anchor" href="#伪类选择器" aria-label="Permalink to &quot;伪类选择器&quot;">​</a></h3><table tabindex="0"><thead><tr><th style="text-align:left;">选择器</th><th style="text-align:left;">例子</th><th style="text-align:left;">例子描述</th></tr></thead><tbody><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_active.asp" target="_blank" rel="noreferrer">:active</a></td><td style="text-align:left;">a:active</td><td style="text-align:left;">选择活动的链接。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_focus.asp" target="_blank" rel="noreferrer">:focus</a></td><td style="text-align:left;">input:focus</td><td style="text-align:left;">选择获得焦点的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_hover.asp" target="_blank" rel="noreferrer">:hover</a></td><td style="text-align:left;">a:hover</td><td style="text-align:left;">选择鼠标悬停其上的链接。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_link.asp" target="_blank" rel="noreferrer">:link</a></td><td style="text-align:left;">a:link</td><td style="text-align:left;">选择所有未被访问的链接。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_checked.asp" target="_blank" rel="noreferrer">:checked</a></td><td style="text-align:left;">input:checked</td><td style="text-align:left;">选择每个被选中的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_disabled.asp" target="_blank" rel="noreferrer">:disabled</a></td><td style="text-align:left;">input:disabled</td><td style="text-align:left;">选择每个被禁用的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_enabled.asp" target="_blank" rel="noreferrer">:enabled</a></td><td style="text-align:left;">input:enabled</td><td style="text-align:left;">选择每个已启用的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_first-child.asp" target="_blank" rel="noreferrer">:first-child</a></td><td style="text-align:left;">p:first-child</td><td style="text-align:left;">选择作为其父的首个子元素的每个 <code>&lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_first-of-type.asp" target="_blank" rel="noreferrer">:first-of-type</a></td><td style="text-align:left;">p:first-of-type</td><td style="text-align:left;">选择作为其父的首个<code>&lt;p&gt;</code>元素的每个<code>&lt;p&gt;</code>元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_in-range.asp" target="_blank" rel="noreferrer">:in-range</a></td><td style="text-align:left;">input:in-range</td><td style="text-align:left;">选择具有指定范围内的值的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_invalid.asp" target="_blank" rel="noreferrer">:invalid</a></td><td style="text-align:left;">input:invalid</td><td style="text-align:left;">选择所有具有无效值的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_last-child.asp" target="_blank" rel="noreferrer">:last-child</a></td><td style="text-align:left;">p:last-child</td><td style="text-align:left;">选择作为其父的最后一个子元素的每个<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_last-of-type.asp" target="_blank" rel="noreferrer">:last-of-type</a></td><td style="text-align:left;">p:last-of-type</td><td style="text-align:left;">选择作为其父的最后一个 <code>&lt;p&gt; </code>元素的每个 <code>&lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_not.asp" target="_blank" rel="noreferrer">:not(<em>selector</em>)</a></td><td style="text-align:left;">:not(p)</td><td style="text-align:left;">选择每个非 <code>&lt;p&gt;</code> 元素的元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_nth-child.asp" target="_blank" rel="noreferrer">:nth-child(<em>n</em>)</a></td><td style="text-align:left;">p:nth-child(2)</td><td style="text-align:left;">选择作为其父的第二个子元素的每个<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_nth-last-child.asp" target="_blank" rel="noreferrer">:nth-last-child(<em>n</em>)</a></td><td style="text-align:left;">p:nth-last-child(2)</td><td style="text-align:left;">选择作为父的第二个子元素的每个<code>&lt;p&gt;</code>元素，从最后一个子元素计数。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_nth-last-of-type.asp" target="_blank" rel="noreferrer">:nth-last-of-type(<em>n</em>)</a></td><td style="text-align:left;">p:nth-last-of-type(2)</td><td style="text-align:left;">选择作为父的第二个<code>&lt;p&gt;</code>元素的每个<code>&lt;p&gt;</code>元素，从最后一个子元素计数</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_nth-of-type.asp" target="_blank" rel="noreferrer">:nth-of-type(<em>n</em>)</a></td><td style="text-align:left;">p:nth-of-type(2)</td><td style="text-align:left;">选择作为其父的第二个<code> &lt;p&gt;</code> 元素的每个<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_nth-child.asp" target="_blank" rel="noreferrer">:nth-child(<em>n</em>)</a></td><td style="text-align:left;">p:nth-child(2)</td><td style="text-align:left;">选择作为其父的第二个子元素的每个<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_only-of-type.asp" target="_blank" rel="noreferrer">:only-of-type</a></td><td style="text-align:left;">p:only-of-type</td><td style="text-align:left;">选择作为其父的唯一 <code>&lt;p&gt; </code>元素的每个<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_only-child.asp" target="_blank" rel="noreferrer">:only-child</a></td><td style="text-align:left;">p:only-child</td><td style="text-align:left;">选择作为其父的唯一子元素的<code> &lt;p&gt;</code> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_optional.asp" target="_blank" rel="noreferrer">:optional</a></td><td style="text-align:left;">input:optional</td><td style="text-align:left;">选择不带 &quot;required&quot; 属性的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_out-of-range.asp" target="_blank" rel="noreferrer">:out-of-range</a></td><td style="text-align:left;">input:out-of-range</td><td style="text-align:left;">选择值在指定范围之外的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_read-only.asp" target="_blank" rel="noreferrer">:read-only</a></td><td style="text-align:left;">input:read-only</td><td style="text-align:left;">选择指定了 &quot;readonly&quot; 属性的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_read-write.asp" target="_blank" rel="noreferrer">:read-write</a></td><td style="text-align:left;">input:read-write</td><td style="text-align:left;">选择不带 &quot;readonly&quot; 属性的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_required.asp" target="_blank" rel="noreferrer">:required</a></td><td style="text-align:left;">input:required</td><td style="text-align:left;">选择指定了 &quot;required&quot; 属性的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_root.asp" target="_blank" rel="noreferrer">:root</a></td><td style="text-align:left;">root</td><td style="text-align:left;">选择元素的根元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_target.asp" target="_blank" rel="noreferrer">:target</a></td><td style="text-align:left;">#news:target</td><td style="text-align:left;">选择当前活动的 #news 元素（单击包含该锚名称的 URL）。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_valid.asp" target="_blank" rel="noreferrer">:valid</a></td><td style="text-align:left;">input:valid</td><td style="text-align:left;">选择所有具有有效值的 <input> 元素。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_visited.asp" target="_blank" rel="noreferrer">:visited</a></td><td style="text-align:left;">a:visited</td><td style="text-align:left;">选择所有已访问的链接。</td></tr></tbody></table><h3 id="伪元素选择器" tabindex="-1">伪元素选择器 <a class="header-anchor" href="#伪元素选择器" aria-label="Permalink to &quot;伪元素选择器&quot;">​</a></h3><table tabindex="0"><thead><tr><th style="text-align:left;">选择器</th><th style="text-align:left;">例子</th><th style="text-align:left;">例子描述</th></tr></thead><tbody><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_after.asp" target="_blank" rel="noreferrer">::after</a></td><td style="text-align:left;">p::after</td><td style="text-align:left;">在每个<code>&lt;p&gt;</code>元素之后插入内容。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_before.asp" target="_blank" rel="noreferrer">::before</a></td><td style="text-align:left;">p::before</td><td style="text-align:left;">在每个 <code>&lt;p&gt; </code>元素之前插入内容。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_first-letter.asp" target="_blank" rel="noreferrer">::first-letter</a></td><td style="text-align:left;">p::first-letter</td><td style="text-align:left;">选择每个<code>&lt;p&gt;</code>元素的首字母。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_first-line.asp" target="_blank" rel="noreferrer">::first-line</a></td><td style="text-align:left;">p::first-line</td><td style="text-align:left;">选择每个<code>&lt;p&gt;</code>元素的首行。</td></tr><tr><td style="text-align:left;"><a href="https://www.w3school.com.cn/cssref/selector_selection.asp" target="_blank" rel="noreferrer">::selection</a></td><td style="text-align:left;">p::selection</td><td style="text-align:left;">选择用户选择的元素部分。</td></tr></tbody></table><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">    div</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">        border: </span><span style="color:#D19A66;">solid</span><span style="color:#D19A66;"> 1</span><span style="color:#E06C75;">px</span><span style="color:#D19A66;"> #ddd</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        width: </span><span style="color:#D19A66;">150</span><span style="color:#E06C75;">px</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    div</span><span style="color:#ABB2BF;">&gt;</span><span style="color:#E06C75;">input</span><span style="color:#C678DD;">[</span><span style="color:#D19A66;">type</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;text&quot;</span><span style="color:#C678DD;">]</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">        border: </span><span style="color:#D19A66;">none</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        outline: </span><span style="color:#D19A66;">none</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    div</span><span style="color:#ABB2BF;">&gt;</span><span style="color:#E06C75;">input</span><span style="color:#C678DD;">[</span><span style="color:#D19A66;">type</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;text&quot;</span><span style="color:#C678DD;">]</span><span style="color:#ABB2BF;">+</span><span style="color:#E06C75;">span</span><span style="color:#56B6C2;">:after</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">        content: </span><span style="color:#98C379;">&quot;</span><span style="color:#56B6C2;">\21AA</span><span style="color:#98C379;">&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        font-size: </span><span style="color:#D19A66;">14</span><span style="color:#E06C75;">px</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        cursor: </span><span style="color:#D19A66;">pointer</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">...</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">	&lt;</span><span style="color:#E06C75;">input</span><span style="color:#D19A66;"> type</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;text&quot;</span><span style="color:#ABB2BF;">&gt;&lt;</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="元素权重" tabindex="-1">元素权重 <a class="header-anchor" href="#元素权重" aria-label="Permalink to &quot;元素权重&quot;">​</a></h2><p>元素会被多个样式一层层作用，这就是层叠样式表的来源。如果多个样式做用在元素上就会产生优先级权重问题。</p><p>使用类、ID、伪类都有不同的权重，具体应用哪条规则要看权限大小。</p><ul><li>相同权重的规则应用最后出现的</li><li>可以使用 <code>!important</code> 强制提升某个规则的权限</li></ul><h3 id="权重应用" tabindex="-1">权重应用 <a class="header-anchor" href="#权重应用" aria-label="Permalink to &quot;权重应用&quot;">​</a></h3><table tabindex="0"><thead><tr><th style="text-align:left;">规则</th><th style="text-align:left;">粒度</th></tr></thead><tbody><tr><td style="text-align:left;">*</td><td style="text-align:left;">0000</td></tr><tr><td style="text-align:left;">标签,伪元素</td><td style="text-align:left;">0001</td></tr><tr><td style="text-align:left;">class，类属性值</td><td style="text-align:left;">0010</td></tr><tr><td style="text-align:left;">ID</td><td style="text-align:left;">0100</td></tr><tr><td style="text-align:left;">行内样式</td><td style="text-align:left;">1000</td></tr></tbody></table><h3 id="强制优先级" tabindex="-1">强制优先级 <a class="header-anchor" href="#强制优先级" aria-label="Permalink to &quot;强制优先级&quot;">​</a></h3><p>有时在规则冲突时，为了让某个规则强制有效可以使用 !important。</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">  h2</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;"> 	 color: </span><span style="color:#D19A66;">red</span><span style="color:#C678DD;"> !important</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  h2</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;"> 	 color: </span><span style="color:#D19A66;">green</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;important&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;!</span><span style="color:#E06C75;">DOCTYPE</span><span style="color:#D19A66;"> html</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">html</span><span style="color:#D19A66;"> lang</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;en&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">head</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;</span><span style="color:#E06C75;">meta</span><span style="color:#D19A66;"> charset</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;UTF-8&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;</span><span style="color:#E06C75;">meta</span><span style="color:#D19A66;"> name</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;viewport&quot;</span><span style="color:#D19A66;"> content</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;width=device-width, initial-scale=1.0&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;</span><span style="color:#E06C75;">title</span><span style="color:#ABB2BF;">&gt;权重&lt;/</span><span style="color:#E06C75;">title</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#61AFEF;">        #id</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">            color: </span><span style="color:#D19A66;">blue</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">        h2</span><span style="color:#D19A66;">.a.b.c.d.e.f.g.h.i.j.k.l.m.n</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">            color: </span><span style="color:#D19A66;">red</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        }</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">head</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">body</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">    &lt;</span><span style="color:#E06C75;">h2</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;id&quot;</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;a b c d e f g h i j k l m n&quot;</span><span style="color:#ABB2BF;">&gt;任然显示绿色&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">body</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">html</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h2 id="继承规则" tabindex="-1">继承规则 <a class="header-anchor" href="#继承规则" aria-label="Permalink to &quot;继承规则&quot;">​</a></h2><p>子元素可以继承父元素设置的样式。</p><ul><li>子元素并不是全部样式。比如边框、高度等并不会继承。</li><li>继承的规则没有权重</li></ul><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">  article</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">    color: </span><span style="color:#D19A66;">red</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">    border: </span><span style="color:#D19A66;">solid</span><span style="color:#D19A66;"> 1</span><span style="color:#E06C75;">px</span><span style="color:#D19A66;"> #ddd</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">...</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">	&lt;</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;继承颜色 &lt;</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;没有继承边框&lt;/</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="通配符" tabindex="-1">通配符 <a class="header-anchor" href="#通配符" aria-label="Permalink to &quot;通配符&quot;">​</a></h2><p>通配符的权限为 0，而继承的规则没有权重即比0的权重还低。</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">  *</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">  	color: </span><span style="color:#D19A66;">red</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  h2</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">  	color: </span><span style="color:#D19A66;">blue</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">	&lt;</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;blue &lt;</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;红色&lt;/</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><code>h2</code> 中的 <code>span</code> 并没有继承 <code>h2</code> 的颜色，就是因为继承没有权重。而使用了 <code>*</code> 权重为 0 的规则。如果想<code>span</code>继承h2，一般的操作是将统配替换为<code>html</code> 或者 <code>:root</code>。</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">  html</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">  	color: </span><span style="color:#D19A66;">red</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  h2</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">  	color: </span><span style="color:#D19A66;">blue</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">	&lt;</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;blue &lt;</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;红色&lt;/</span><span style="color:#E06C75;">span</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">article</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/frontend/css/%E8%83%8C%E6%99%AF.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>背景</span><!--]--></a></div><div class="pager" data-v-e257564d><!----></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>